import cv2
import numpy as np

def Circle(path, thresh , dp, minDist, param2, ro):
    if __name__ == '__main__':
        image_np = cv2.imread(path)
        image_np_gray = cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY)
        _, image_np_thresh = cv2.threshold(image_np_gray, thresh, 255, cv2.THRESH_BINARY)
        image_edges = cv2.Canny(image_np_thresh, 30, 70)
        drawing1 = np.zeros(image_np.shape, dtype=np.uint8)
        drawing2 = np.zeros(image_np.shape, dtype=np.uint8)
        circles = cv2.HoughCircles(image_edges, cv2.HOUGH_GRADIENT, dp, minDist, param2 = param2)
        print(circles[0])
        for circle in circles[0]:
            print(circle)
            x, y, r = circle
                 # 画圆
            cv2.circle(drawing1, (int(x), int(y)), int(r), (0, 0, 255), 2, lineType=cv2.LINE_AA)
            # 画圆心
            cv2.circle(drawing1, (int(x), int(y)), 1, (0, 0, 255), 2, lineType=cv2.LINE_AA)

            if r == ro:
                cv2.circle(drawing2, (int(x), int(y)), int(r), (0, 0, 255), 2, lineType = cv2.LINE_AA)
                # 画圆心
                cv2.circle(drawing2, (int(x), int(y)), 1, (0, 0, 255), 2, lineType = cv2.LINE_AA)

        cv2.imshow('', image_np_thresh)
        cv2.imshow('', drawing1)
        cv2.imshow('', drawing1)
        cv2.imwrite('qiu1.jpg', drawing1)
        cv2.imwrite('qiu2.jpg', drawing2)
        cv2.imwrite('qiu0.jpg', image_np_thresh)
        cv2.waitKey(0)
        return drawing2



drawing = Circle('work1.1.jpg', thresh = 100, dp= 20, minDist = 80, param2 = 520, ro= 28)
cv2.imshow('', drawing)
cv2.imwrite('zuoye1.1.jpg', drawing)
cv2.waitKey(0)
drawing = Circle('work1.2.jpg', thresh = 80, dp= 30, minDist = 80, param2 = 1230, ro= 54)
cv2.imshow('', drawing)
cv2.imwrite('zuoye1.2.jpg', drawing)
cv2.waitKey(0)